package seg.tef4.whms.util.gesturelistener;

import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

public abstract class GestureListener extends GestureDetector.SimpleOnGestureListener implements View.OnTouchListener {
    /**
     * 左右滑动的最短距离
     */
    private int distance;
    /**
     * 左右滑动的最大速度
     */
    private int velocity;

    private GestureDetector gestureDetector;

    public GestureListener(Context context, int distance, int velocity) {
        super();
        gestureDetector = new GestureDetector(context, this);
        this.distance = distance;
        this.velocity = velocity;
    }

    public GestureListener(Context context) {
        super();
        gestureDetector = new GestureDetector(context, this);
        this.distance = 100;
        this.velocity = 200;
    }

    protected abstract void left();

    protected abstract void right();

    /**
     * @param e1        第1个ACTION_DOWN MotionEvent
     * @param e2        最后一个ACTION_MOVE MotionEvent
     * @param velocityX X轴上的移动速度（像素/秒）
     * @param velocityY Y轴上的移动速度（像素/秒）
     * @return
     */
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                           float velocityY) {
        // 向左滑
        if (e1.getX() - e2.getX() > distance
                && Math.abs(velocityX) > velocity) {
            left();
        }
        // 向右滑
        if (e2.getX() - e1.getX() > distance
                && Math.abs(velocityX) > velocity) {
            right();
        }

        return false;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        gestureDetector.onTouchEvent(event);
        return false;
    }

    public int getDistance() {
        return distance;
    }

    public void setDistance(int distance) {
        this.distance = distance;
    }

    public int getVelocity() {
        return velocity;
    }

    public void setVelocity(int velocity) {
        this.velocity = velocity;
    }

    public GestureDetector getGestureDetector() {
        return gestureDetector;
    }

    public void setGestureDetector(GestureDetector gestureDetector) {
        this.gestureDetector = gestureDetector;
    }

}
